<?php
	include_once("dbi.php");

	$user_logged = isset($_SESSION[''.SECURITY_SESSION.'']['username']) ? $_SESSION[''.SECURITY_SESSION.'']['username'] : FALSE;

	echo "<div id='text'><a class='title'>My Reservations » ".$user_logged."</a></div>";
	
	if ($user_logged){
		$now_date = date("Y-m-d H:i:s", mktime(date("H"), date("i")+5, 0, date("m"), date("d"), date("Y")));
		
		//Display My Nodes/Channels
		require("mysql_connection.php");
		$query = mysql_query("SELECT * FROM ".DB_SCHEDULER.".`islands` ORDER BY `institution` ASC");
			
		$error = 0;
		while($results = mysql_fetch_object($query)){
			$connection = array('HOST' => ''.$results->ip_address.'', 'USER' => ''.ISLAND_USER_MYSQL.'', 'PASS' => ''.ISLAND_PASS_MYSQL.'', 'INVENTORY_DB' => ''.$results->inventory_db.'', 'SCHEDULER_DB' => ''.$results->scheduler_db.'');

			$cancel_n = $_REQUEST['cancel_n'];
			$c_reserve_id = $_REQUEST['reservation_id'];
			if($cancel_n && ($_REQUEST['island'] == $results->institution)) { cancelReservation_Id($c_reserve_id, $connection); }

			$cancel_c = $_REQUEST['cancel_c'];
			$c_spec_id = $_REQUEST['spec_reserve_id'];
			if($cancel_c && ($_REQUEST['island'] == $results->institution)) { cancelSpecReservation_Id($c_spec_id, $connection); }
		
			$my_nodes = getMyNodes($user_logged, $connection);
			$my_channels = getMyChannels($user_logged, $connection);
			
			$numOfNodes = count($my_nodes);
			$numOfChannels = count($my_channels);
			
			echo "<div class='institution_title2'>My Reservations from ".utf8_encode($results->name)."</div>";
			
			echo "<table id='reserve' class='table_max'>
					<tr class='top'>
						<td>Node</td>
						<td>Begin Time</td>
						<td>End Time</td>
						<td>Actions</td>
					</tr>";
					
			if($numOfNodes == 0){
				echo "<tr><td colspan='4'>You Have not a Node Reservation in ".utf8_encode($results->name)."</td></tr>";
			}
			
			for($i = 0; $i < $numOfNodes; $i++){
				$node_description = "<center><b>Node description:</b></center>" .
									"<b>Hostname:</b> ".$my_nodes[$i]['node_name']."<br>" .
									"<b>Control IP:</b> ".$my_nodes[$i]['control_ip']."";

				echo "<tr>
					<td><span onMouseOver=\"toolTip('".$node_description."', 200)\" onMouseOut=\"toolTip()\">Node ".$my_nodes[$i]['node_id']."</span></td>
					<td>".$my_nodes[$i]['begin_time']."</td>
					<td>".$my_nodes[$i]['end_time']."</td>
					<td>
						<form method='POST' action=''>
							<input type='hidden' name='island' value='".$results->institution."'>
							<input type='hidden' name='reservation_id' value='".$my_nodes[$i]['reservation_id']."'>
							<input type='submit' name='cancel_n' value='Cancel'>
						</form>
					</td>
				</tr>";
			}
			
			echo "</table>";
			
			echo "<table id='reserve' class='table_max'>
					<tr class='top'>
						<td>Channel</td>
						<td>Begin Time</td>
						<td>End Time</td>
						<td>Actions</td>
					</tr>";
					
			if($numOfChannels == 0){
				echo "<tr><td colspan='4'>You Have not a Channel Reservation in ".utf8_encode($results->name)."</td></tr>";
			}
			
			for($i = 0; $i < $numOfChannels; $i++){
					echo "<tr>
						<td>Channel ".$my_channels[$i]['channel']."</td>
						<td>".$my_channels[$i]['begin_time']."</td>
						<td>".$my_channels[$i]['end_time']."</td>
						<td>
							<form method='POST' action=''>
								<input type='hidden' name='island' value='".$results->institution."'>
								<input type='hidden' name='spec_reserve_id' value='".$my_channels[$i]['id']."'>
								<input type='submit' name='cancel_c' value='Cancel'>
							</form>
						</td>
					</tr>";
			}
			
			echo "</table>";
			
			mysql_close();
		}
	}else{
		set_message('You do not have a slice! Please contact LABORA for a slice creation!','error',1);
	}
?>		
